######################################
# Generate plots using conjoint data
######################################

rm(list=ls())

sink("002_conjoint_plots.txt")

library("ggplot2")
library(gridExtra)

####################
# All respondents
####################

# load data
d <- read.table("results_502_subjects.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 12, color = "black")
f1 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5))

####################
# Rural
####################

# load data
d <- read.table("results_rural_subjects.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f2 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Rural subjects") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5)) 

####################
# Urban
####################

# load data
d <- read.table("results_urban_subjects.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f3 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Urban subjects") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5)) + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank())

###################### 
# Less than 900 RON
######################

# load data
d <- read.table("results_lessthan900ron_subjects.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f4 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Lower income") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + theme(plot.title = element_text(hjust = 0.5))

###################### 
# More than 900 RON
######################

# load data
d <- read.table("results_morethan900ron_subjects.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f5 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Higher income") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5))

#########################
# Programmatic candidate
#########################

# load data
d <- read.table("results_programmatic_candidates.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  
  
  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No money",
                                                   "Vote-buying",
                                                   "Welfare favors"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(10,11,12,13,14,15,16,17,1,2,3,4,5,6,7,8,9,22,23,24,25,21,18,19,20)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f6 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Programmatic candidates") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5)) + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank())

#########################
# Nonprogrammatic candidate
#########################

# load data
d <- read.table("results_nonprogrammatic_candidates.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  
  
  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No money",
                                                   "Vote-buying",
                                                   "Welfare favors"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(10,11,12,13,14,15,16,17,1,2,3,4,5,6,7,8,9,22,23,24,25,21,18,19,20)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f7 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Non-programmatic candidates") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5))

###################### 
# Delta urban
######################

# load data
d <- read.table("results_delta_rural_urban.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f8 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Difference rural-urban") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5)) + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank())

##############################
# Delta less than 900 RON
##############################

# load data
d <- read.table("results_delta_more900_less900.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Policy      <- paste(c("1b",2,3),".atpol",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Policy] <- "Policy"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  

  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No particularistic offer",
                                                   "Vote-buying",
                                                   "Welfare favor"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Policy] <- paste(offset,c("No promises",
                                             "Renovate schools",
                                             "Renovate schools and roads"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "POLICY/PUBLIC GOODS:",
                        "      ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5,15.1,15.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(15,16,17,18,19,20,21,22,1,2,3,4,5,6,7,8,9,27,28,29,30,26,10,11,12,13,14,23,24,25)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f9 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Difference higher-lower income") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5))

##############################
# Delta Programmatic
##############################

# load data
d <- read.table("results_delta_nonprog_prog.txt",comment.char="")

# group vars
Gender     <- paste(c("1b",2),".atgen",sep="")
Experience    <- paste(c("1b",2),".atexp",sep="")
Coercion    <- paste(c("1b",2),".atinti",sep="")
Clientelism   <- paste(c("1b",2,3),".atmita",sep="")
Integrity       <- paste(c("1b",2,3),".atinte",sep="")
Income     <- paste(c("1b",2),".atven",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Gender ]     <- "Gender"
  d$gruppe[d$var %in% Experience]   <- "Experience"
  d$gruppe[d$var %in% Coercion]       <- "Coercion"
  d$gruppe[d$var %in% Clientelism]          <- "Clientelism"
  d$gruppe[d$var %in% Integrity]   <- "Integrity"
  d$gruppe[d$var %in% Income] <- "Income"
  
  # reorder
  d <- rbind(d[d$var %in% Gender,],
             d[d$var %in% Experience,],
             d[d$var %in% Coercion,],
             d[d$var %in% Clientelism,],
             d[d$var %in% Integrity,],
             d[d$var %in% Policy,],
             d[d$var %in% Income,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
    d$var[d$var %in% Gender] <- paste(offset,c("Male",
                                             "Female"))
  
  
  d$var[d$var %in% Experience] <- paste(offset,c("No experience as mayor",
                                                 "Previous experience as mayor"))
  
  d$var[d$var %in% Coercion] <- paste(offset,c("No threat",
                                               "Welfare coercion"))
  
  d$var[d$var %in% Clientelism] <-  paste(offset,c("No money",
                                                   "Vote-buying",
                                                   "Welfare favors"))
  
  d$var[d$var %in% Integrity] <-paste(offset,c("No irregularities",
                                               "Investigated",
                                               "Sentenced"))
  
  d$var[d$var %in% Income] <- paste(offset,c("Income not excessively high",
                                             "High income"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("GENDER:",
                        " ",
                        "POLITICAL EXPERIENCE:",
                        "  ",
                        "NEGATIVE INDUCEMENT:",
                        "   ",
                        "POSITIVE INDUCEMENT:",
                        "    ",
                        "INVESTIGATION:",
                        "     ",
                        "INCOME:"
),order=c(0.5,2.1,2.5,4.1,4.5,6.1,6.5,9.1,9.5,12.1,12.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$order2 = c(10,11,12,13,14,15,16,17,1,2,3,4,5,6,7,8,9,22,23,24,25,21,18,19,20)
d <-d[order(d$order2),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

ttext <- element_text(face = "bold", size = 15, color = "black")
f10 = ggplot(d,aes(y=pe,x=var)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "Difference nonprogrammatic-programmatic") + coord_flip(ylim = c(-.4, .4)) + geom_pointrange(aes(ymin=lower,ymax=upper,width=.4),size=.6,position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.4,.4,.2),1),labels=c("-.4","-.2","0",".2",".4")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(title = ttext, axis.title = ttext) + theme(axis.text=ttext) + theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()) + theme(axis.text=ttext) + theme(plot.title = element_text(hjust = 0.5))

##############
# Save plots
##############

# FIGURE 1
pdf('001_plot_all_subjects.pdf',width = 7,height=8)
grid.arrange(f1, ncol = 1, top = "")
dev.off()

# FIGURE 2
cairo_pdf(file="002_final_income_interacion.pdf", 
          width=25, 
          height=15)
grid.arrange(cbind(ggplotGrob(f5), ggplotGrob(f4), ggplotGrob(f9), size="last"))
dev.off()


# APPENDIX H
cairo_pdf(file="003_final_urban_interacion.pdf", 
          width=25, 
          height=15)
grid.arrange(cbind(ggplotGrob(f2), ggplotGrob(f3), ggplotGrob(f8), size="last"))
dev.off()

# APPENDIX H
cairo_pdf(file="004_final_programmatic_interacion.pdf", 
          width=25, 
          height=15)
grid.arrange(cbind(ggplotGrob(f7), ggplotGrob(f6), ggplotGrob(f10), size="last"))
dev.off()

sink()

